from selenium.webdriver.common.by import By

from base.mp.base_page import BasePage, BaseOperate
from utils import UtilsDriver, select_box


class PublishPage(BasePage):
    def __init__(self):
        super().__init__()
        self.iframe = By.ID, "publishTinymce_ifr"
        # 文章标题
        self.title = By.CSS_SELECTOR, "[placeholder='文章名称']"
        # 文章内容
        self.content = By.CSS_SELECTOR, "#tinymce"
        # 封面选择
        self.cover = By.XPATH, "//*[@class='el-radio-group']/label[3]/span"
        # 频道选择
        self.channel = By.CSS_SELECTOR, "[placeholder='请选择']"
        # 发表按钮
        self.publish_btn = By.XPATH, "//*[text()='发表']"
        # 发表成功提示信息
        self.msg = By.CSS_SELECTOR, ".el-message__content"

    def find_title_box(self):
        return self.get_element(self.title)

    def find_content_body(self):
        return self.get_element(self.content)

    def find_img_select(self):
        return self.get_element(self.cover)

    # 下拉框
    def find_channel(self):
        return self.get_element(self.channel)

    def find_publish_btn(self):
        return self.get_element(self.publish_btn)

    def find_msg(self):
        return self.get_element(self.msg)


class PublishOperate(BaseOperate):
    def __init__(self):
        self.pb_page = PublishPage()
        self.driver = UtilsDriver.get_mp_driver()

    def input_title(self, title):
        self.input_text(self.pb_page.find_title_box(), title)

    def input_content(self, content):
        self.driver.switch_to.frame("publishTinymce_ifr")
        self.input_text(self.pb_page.find_content_body(), content)
        self.driver.switch_to.default_content()

    # 无图
    def select_img(self):
        self.pb_page.find_img_select().click()

    # 下拉框选择
    def select_channel(self, content_value):
        select_box(self.driver, self.pb_page.find_channel(), content_value)

    def click_publish_btn(self):
        self.pb_page.find_publish_btn().click()

    def get_msg(self):
        return self.pb_page.find_msg().text


class PublishBusiness:
    def __init__(self):
        self.operate = PublishOperate()

    def publish_article(self, title, content, content_value):
        # 输入标题
        self.operate.input_title(title)
        # 输入内容
        self.operate.input_content(content)
        # 点击无图片框
        self.operate.select_img()
        # 下拉框选择
        self.operate.select_channel(content_value)
        # 点击发表
        self.operate.click_publish_btn()

    def get_msg_info(self):
        return self.operate.get_msg()
